package com.fancy.learncenter.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.fancy.learncenter.activity.GalleryPicDetailActivity;
import com.fancy.learncenter.activity.PersonInfoActivity;
import com.fancy.learncenter.activity.TzDetailActivity;
import com.fancy.learncenter.activity.YgtPicListActivity;
import com.fancy.learncenter.adapter.base.CommonRecycleViewAdapter;
import com.fancy.learncenter.adapter.base.CustomViewHold;
import com.fancy.learncenter.bean.TzDataBean;
import com.fancy.learncenter.utils.SPUtils;
import com.fancy.learncenter.utils.Utils;
import com.superservice.lya.R;

import java.util.ArrayList;

/**
 * Created by hyy on 2018/1/27.
 * Explain activity as
 */

public class ToothTZAdapter extends CommonRecycleViewAdapter<TzDataBean> {

    public ToothTZAdapter(Context mContext, ArrayList<TzDataBean> itemDatas) {
        super(mContext, R.layout.fragment_tooth_tz_item, itemDatas);
    }

    boolean isMy = false;

    public ToothTZAdapter(Context mContext, ArrayList<TzDataBean> itemDatas, boolean isMy) {
        super(mContext, R.layout.fragment_tooth_tz_item, itemDatas);

        this.isMy = isMy;
        widh = Utils.getScreenWidth(mContext);
    }

    int widh = 0;

    @Override
    public void bindView(CustomViewHold customViewHold, final TzDataBean item, final int position) {

        SimpleDraweeView simpleDraweeView = customViewHold.getView(R.id.simpleDraweeView);
        simpleDraweeView.setImageURI(item.getImgsrc());
        simpleDraweeView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, PersonInfoActivity.class);
                intent.putExtra("userId", "" + item.getUser_id());
                mContext.startActivity(intent);
            }
        });


        TextView name = customViewHold.getView(R.id.name);
        name.setText(item.getNick_name());


        TextView content = customViewHold.getView(R.id.content);
        content.setText(item.getTitle());

        TextView time = customViewHold.getView(R.id.time);
        time.setText(item.getAdd_time());

        TextView answer_num = customViewHold.getView(R.id.answer_num);
        answer_num.setText(item.getComment_num() + "人回帖");

        ImageView delete_img = customViewHold.getView(R.id.delete_img);
        if (isMy) {
            delete_img.setVisibility(View.VISIBLE);
        } else {
            delete_img.setVisibility(View.GONE);
        }

        delete_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (deleteCallBack != null) {
                    deleteCallBack.deleteCallBack(position, item.getId());
                }
            }
        });
        SimpleDraweeView pic_1 = customViewHold.getView(R.id.pic_1);
        SimpleDraweeView pic_2 = customViewHold.getView(R.id.pic_2);
        SimpleDraweeView pic_3 = customViewHold.getView(R.id.pic_3);

        if (item.getThumbList() != null) {


            if (item.getThumbList().size() == 1) {
                pic_1.setVisibility(View.VISIBLE);
                pic_2.setVisibility(View.INVISIBLE);
                pic_3.setVisibility(View.INVISIBLE);


                pic_1.setImageURI(item.getThumbList().get(0));
            } else if (item.getThumbList().size() == 2) {
                pic_1.setVisibility(View.VISIBLE);
                pic_2.setVisibility(View.VISIBLE);
                pic_3.setVisibility(View.INVISIBLE);

                pic_1.setImageURI(item.getThumbList().get(0));
                pic_2.setImageURI(item.getThumbList().get(1));
            } else if ((item.getThumbList().size() >= 3)) {
                pic_1.setVisibility(View.VISIBLE);
                pic_2.setVisibility(View.VISIBLE);
                pic_3.setVisibility(View.VISIBLE);

                pic_1.setImageURI(item.getThumbList().get(0));
                pic_2.setImageURI(item.getThumbList().get(1));
                pic_3.setImageURI(item.getThumbList().get(2));
            }

        } else {
            pic_1.setVisibility(View.GONE);
            pic_2.setVisibility(View.GONE);
            pic_3.setVisibility(View.GONE);
        }
        pic_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, GalleryPicDetailActivity.class);
                intent.putStringArrayListExtra(GalleryPicDetailActivity.PIC_KEY, (ArrayList<String>) item.getImglist());
                intent.putExtra(GalleryPicDetailActivity.PIC_POSITION_KEY, 0);
                mContext.startActivity(intent);
            }
        });
        pic_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, GalleryPicDetailActivity.class);
                intent.putStringArrayListExtra(GalleryPicDetailActivity.PIC_KEY, (ArrayList<String>) item.getImglist());
                intent.putExtra(GalleryPicDetailActivity.PIC_POSITION_KEY, 1);
                mContext.startActivity(intent);
            }
        });
        pic_3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, GalleryPicDetailActivity.class);
                intent.putStringArrayListExtra(GalleryPicDetailActivity.PIC_KEY, (ArrayList<String>) item.getImglist());
                intent.putExtra(GalleryPicDetailActivity.PIC_POSITION_KEY, 2);
                mContext.startActivity(intent);
            }
        });


        LinearLayout rootView = customViewHold.getView(R.id.rootView);
        rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, TzDetailActivity.class);

                intent.putExtra("wdListDataBean", item.getId());

                mContext.startActivity(intent);
            }
        });
    }

    private DeleteCallBack deleteCallBack;

    public DeleteCallBack getDeleteCallBack() {
        return deleteCallBack;
    }

    public void setDeleteCallBack(DeleteCallBack deleteCallBack) {
        this.deleteCallBack = deleteCallBack;
    }

    public interface DeleteCallBack {
        abstract void deleteCallBack(int position, String forumId);
    }
}
